<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/4 0004
 * Time: 17:41
 */

namespace app\admin\controller;


use app\common\model\Category as CategoryModel;
use app\common\validate\CategoryValidate;
use app\common\validate\IDMustBePostiveInt;
use think\Exception;
use think\facade\Request;
use app\common\model\CategoryAndBrand as CategoryAndBrandModel;

class Category extends Common
{

    /**
     * 分类列表
     * @return \think\response\Json|\think\response\View
     * @throws \think\exception\DbException
     */
    public function index() {
        if (Request::isAjax()) {
            $data = CategoryModel::all(function ($query) {
                $query->order('sort asc, id desc');
            });
            return return_data('分类列表', 0, $data);
        }
        return view();
    }

    /**
     * 保存category
     * @return CategoryModel|array|null|\think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function save_category() {
        $id = input('param.id', 0);
        if (request()->isAjax()) {

            $category_validate = new CategoryValidate();
            $category_validate->goCheck();
            $param = $category_validate->getDataByRule(input('param.'));
            $data = CategoryModel::createOrUpdateCategory($param, $id);
            return $data;
        } else {
            $data = [];
            if ($id > 0) {
                $data = CategoryModel::get($id);
            }
            $this->assign('data', $data);

            // 获取所有一级分类
            $cates = CategoryModel::all(['pid' => 0]);
            $this->assign('cates', $cates);

            return view();
        }
    }

    /**
     * 删除category
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delete_category() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $category = CategoryModel::get($id);
        if (!$category) {
            return return_data('分类不存在');
        }
        try {
            $category->delete();
            return return_data('删除成功！');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }

    }

    /**
     * 修改分类排序
     * @return \think\response\Json
     * @throws Exception
     * @throws \think\exception\DbException
     */
    public function save_sort() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $category = CategoryModel::get($id);
        if (!$category) {
            return return_data('分类不存在');
        }
        try {
            $category->save([
                'sort' => input('param.sort')
            ]);
            return return_data('修改成功！');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 关联品牌
     * @return CategoryModel|null|\think\response\Json|\think\response\View
     * @throws Exception
     * @throws Exception\DbException
     */
    public function relevance_brand() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        if ($this->request->isAjax()) {
            $ids = input('param.ids/a', []);
            $data = CategoryAndBrandModel::categoryRelevanceBrand($id, $ids);
            return $data;
        }

        $data = CategoryModel::get($id);
        $this->assign('data', $data);

        $brand_category = CategoryAndBrandModel::getIds(['category_id' => $id], 'brand_id');
        $this->assign('brand_category', $brand_category);

        // 获取所有分类
        $brands = \app\common\model\Brand::all();
        $this->assign('brands', $brands);
        return view();
    }
}